package com.unibuy.smartdevice.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.SearchView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.unibuy.smartdevice.AppCompatActivityAbstract;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.structure.ProductStructure;
import com.unibuy.smartdevice.database.ProductsDao;
import com.unibuy.smartdevice.databinding.ActivityProductSearchBinding;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.HandlerMainSchedulerOnClick;
import com.unibuy.smartdevice.tools.ToastHandlerMain;
import com.unibuy.smartdevice.ui.recycler.RecyclerProductSearchAdpter;
import com.unibuy.smartdevice.ui.recycler.RecyclerSearchTextAdpter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductSearchActivity extends AppCompatActivityAbstract {
    public enum Option {
        TOAST(0),
        RESET(1),
        ;

        private int option;

        public int getOption() {
            return option;
        }

        Option(int option) {
            this.option = option;
        }
    }

    public static final Map<Integer, Option> optionMap = new HashMap<>();
    static {
        for (Option option : Option.values()) {
            optionMap.put(option.getOption(), option);
        }
    }

    private ActivityProductSearchBinding binding;
    private int position;
    private List<String> searchTextList = new ArrayList<>();
    private RecyclerSearchTextAdpter recyclerSearchTextAdpter;
    private List<ProductStructure> searchProductList = new ArrayList<>();
    private RecyclerProductSearchAdpter recyclerProductSearchAdpter;

    @Override
    protected Context setCtx() {
        return this;
    }

    @Override
    protected Class<?> setCls() {
        return getClass();
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return new ToastHandlerMain(getCtx(), getLogs()) {

            @Override
            protected void execute(Context context, int commandCode, String message) {
                Option option = optionMap.get(commandCode);

                switch (option) {
                    case TOAST:
                        super.execute(context, commandCode, message);
                        break;
                    case RESET:
                        int size = searchProductList.size();
                        searchProductList.clear();
                        if (size > 0) recyclerProductSearchAdpter.notifyItemRangeRemoved(0, size);

                        size = searchTextList.size();
                        searchTextList.clear();
                        if (size > 0) recyclerSearchTextAdpter.notifyItemRangeRemoved(0, size);

                        if (message.isEmpty()) {
                            for (ProductStructure product: MyApp.getInstance().getProductList()) {
                                searchProductList.add(product);
                            }

                            recyclerProductSearchAdpter.notifyItemInserted(searchProductList.size() -1);
                        } else {
                            for (ProductStructure product: MyApp.getInstance().getProductList()) {
                                if (product.getProductName().contains(message)) {
                                    searchProductList.add(product);
                                    String name = product.getProductName();
//                            Log.i("product name", name);
                                    int index = name.indexOf(message);
                                    if (index != -1 && index < name.length() - 1) {
                                        String result = name.substring(index + message.length());

                                        if (!result.isEmpty()) {
                                            String oneText = String.valueOf(result.charAt(0));
                                            if (!searchTextList.contains(oneText)) {
                                                searchTextList.add(oneText);
                                            }
                                        }
                                    }
                                }
                            }

                            if (searchTextList.size() > 2) Collections.sort(searchTextList);
                            recyclerProductSearchAdpter.notifyItemInserted(searchProductList.size() -1);
                            recyclerSearchTextAdpter.notifyItemInserted(searchTextList.size() -1);
                        }
                        break;
                }
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityProductSearchBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Intent intent = getIntent();
        position = intent.getIntExtra("position", -1);

        recyclerProductSearchAdpter = new RecyclerProductSearchAdpter(getCtx(), position, searchProductList);
        binding.recyclerProductSearch.setAdapter(recyclerProductSearchAdpter);
        binding.recyclerProductSearch.setLayoutManager(new LinearLayoutManager(getCtx()));

        recyclerSearchTextAdpter = new RecyclerSearchTextAdpter(binding, searchTextList);
        binding.recyclerSearchText.setAdapter(recyclerSearchTextAdpter);
        binding.recyclerSearchText.setLayoutManager(new LinearLayoutManager(getCtx(), LinearLayoutManager.HORIZONTAL, false));

        binding.searchProductList.setOnQueryTextListener(new SearchProductListOnQueryText(getHandlerMain()));
        binding.buttonBack.setOnClickListener(v -> finish());

        if (MyApp.getInstance().getProductList().isEmpty()) {
            ProductsDao productsDao = new ProductsDao(getCtx());
            for (ProductStructure product: productsDao.getAll()) {
                MyApp.getInstance().getProductList().add(product);
            }
            productsDao.close();
        }

        for (ProductStructure product: MyApp.getInstance().getProductList()) {
            searchProductList.add(product);
        }

        if (!MyApp.getInstance().getProductList().isEmpty()) {
            recyclerProductSearchAdpter.notifyItemInserted(searchProductList.size() - 1);
        }
    }

    public static class SearchProductListOnQueryText extends HandlerMainSchedulerOnClick implements SearchView.OnQueryTextListener {
        private String text;

        public SearchProductListOnQueryText(HandlerMain handlerMain) {
            super(handlerMain);
        }

        @Override
        protected HandlerMain setHandlerMain() {
            return getSrcHandlerMain();
        }

        @Override
        protected void execute(HandlerMain handlerMain) {
            handlerMain.start(getClass().getSimpleName(), Option.RESET.getOption(), text);
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            this.text = query;
            this.start();
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            this.text = newText;
            this.start();
            return false;
        }

        @Override
        public void onClick(View v) {
            start();
        }

        @Override
        public boolean onLongClick(View v) {
            return false;
        }

        @Override
        protected Class<?> setCls() {
            return getClass();
        }
    }
}